// 验证码 服务层
package service

import (
	"admin-go-api/common/util"
	"github.com/mojocn/base64Captcha"
	"image/color"
)

// 使用redis作为容器
var store = util.RedisStore{}

// 生成验证码
func CaptMake() (id, b64s string, code string) {
	var driver base64Captcha.Driver
	var driverString base64Captcha.DriverString
	//	配置验证码信息
	captchaConfig := base64Captcha.DriverString{
		Width:           200,
		Height:          60,
		NoiseCount:      0,                                      //噪声点
		ShowLineOptions: 2 | 4,                                  //分割线
		Length:          6,                                      //验证码长度
		Source:          "1234567890qwertyuiopasdfghjklzxcvbnm", //验证码中包含的字符集
		BgColor: &color.RGBA{
			R: 3,
			G: 102,
			B: 214,
			A: 125,
		},
		Fonts: []string{"wqy-microhei.ttc"},
	}
	driverString = captchaConfig
	driver = driverString.ConvertFonts() //处理字体
	//NewCaptcha方法生成验证码
	captcha := base64Captcha.NewCaptcha(driver, store)
	//Generate方法得到验证码id，base64，验证码本身
	lid, lb64s, lcode, _ := captcha.Generate()
	return lid, lb64s, lcode

}

// 验证验证码是否正确
func CaptVerify(id string, capt string) bool {
	if store.Verify(id, capt, false) {
		return true
	} else {
		return false
	}
}
